<!doctype html>
<!--
Copyright 2014 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="description" content="Sample illustrating the use of WebAudio Offline context rendering promise.">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>WebAudio Offline context rendering promise Sample</title>

    <!-- Add to homescreen for Chrome on Android -->
    <meta name="mobile-web-app-capable" content="yes">
    <link rel="icon" sizes="192x192" href="../images/touch/chrome-touch-icon-192x192.png">

    <!-- Add to homescreen for Safari on iOS -->
    <meta name="apple-mobile-web-app-title" content="WebAudio Offline context rendering promise Sample">

    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" href="../images/apple-touch-icon-precomposed.png">

    <!-- Tile icon for Win8 (144x144 + tile color) -->
    <meta name="msapplication-TileImage" content="images/touch/ms-touch-icon-144x144-precomposed.png">
    <meta name="msapplication-TileColor" content="#3372DF">

    <link rel="icon" href="../images/favicon.ico">

    <link rel="stylesheet" href="../styles/main.css">
  </head>

  <body>
    <h1>WebAudio Offline context rendering promise Sample</h1>

    <p>Available in <a href="https://www.chromestatus.com/feature/5692188675538944">Chrome 42+</a></p>

    <p>
      The <code>OfflineAudioContext.startRendering</code> method renders audio to an <code>AudioBuffer</code> potentially faster than real-time.
      This method returns a <code>Promise</code> which is fulfilled when the rendering is complete.
      It is further documented in the <a href="http://webaudio.github.io/web-audio-api/#widl-OfflineAudioContext-startRendering-Promise-AudioBuffer">Web Audio API</a>.
    </p>

    <div class="output">
      <div>
        Merge two tones, display information about the final <code>AudioBuffer</code>, and plays it.
      </div>
      <button id="start">Start</button>
      <pre id="results"></pre>
    </div>

    <!-- // [START code-block] -->
    <script>

    var seconds = 4;
    var offlineAudioContext = new OfflineAudioContext(2, seconds * 44100, 44100);
    var audioContext = new AudioContext();

    // Fade the tones in/out.
    var gainNode = offlineAudioContext.createGain();
    gainNode.connect(offlineAudioContext.destination);
    gainNode.gain.setValueAtTime(0, 0);
    gainNode.gain.linearRampToValueAtTime(1, 0.5);
    gainNode.gain.setValueAtTime(1, seconds - 0.5);
    gainNode.gain.linearRampToValueAtTime(0, seconds);

    var firstSource = offlineAudioContext.createOscillator();
    firstSource.frequency.value = 200;
    firstSource.connect(gainNode);
    firstSource.start();

    var secondSource = offlineAudioContext.createOscillator();
    secondSource.frequency.value = 350;
    secondSource.connect(gainNode);
    secondSource.start();

    var results = document.getElementById('results');
    var button = document.getElementById('start');

    button.addEventListener('click', function() {
      button.remove();  // remove button from DOM

      var start = +new Date();

      // Render the context, returning a Promise.
      var p = offlineAudioContext.startRendering();
      p.then(function(audioBuffer) {
        console.info('audio result is', audioBuffer);

        var duration = +new Date() - start;
        results.innerText = audioBuffer.toString() + ' in ' + duration + 'ms';

        // Transform to an AudioBufferSourceNode.
        var source = audioContext.createBufferSource();
        source.buffer = audioBuffer;
        source.connect(audioContext.destination);
        return source;
      }).then(function(source) {
        // Play the source.
        source.start();
      });

    });

    </script>
    <!-- // [END code-block] -->

    <script>
      /* jshint ignore:start */
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-53563471-1', 'auto');
      ga('send', 'pageview');
      /* jshint ignore:end */
    </script>
    <!-- Built with love using Web Starter Kit -->
  </body>
</html>
